package cn.zf233.xcloud.commom.enums;

/**
 * Created by zf233 on 2021/8/7
 * 数据单位
 */
public enum DataUnitENUM {

    BYTE("B", 1L),
    KB("KB", DataUnitENUM.BYTE.bytes * 1000L),
    MB("MB", DataUnitENUM.KB.bytes * 1000L),
    GB("GB", DataUnitENUM.MB.bytes * 1000L),
    TB("TB", DataUnitENUM.GB.bytes * 1000L);

    private final String describe; // 描述
    private final Long bytes; // 字节数

    public Long bytes() {
        return this.bytes;
    }

    public Long bytes(Long size) {
        return this.bytes * size;
    }

    public String describe() {
        return this.describe;
    }

    public String string(Long size) {
        size = this.bytes * size;
        StringBuilder builder = new StringBuilder();
        if (size < MB.bytes) {
            long fileSizeFloat = size / KB.bytes;
            builder.append(fileSizeFloat).append(KB.describe);
        } else if (size < GB.bytes) {
            long fileSizeFloat = size / MB.bytes;
            builder.append(fileSizeFloat).append(MB.describe);
        } else if (size < TB.bytes) {
            long fileSizeFloat = size / GB.bytes;
            builder.append(fileSizeFloat).append(GB.describe);
        } else {
            long fileSizeFloat = size / TB.bytes;
            builder.append(fileSizeFloat).append(TB.describe);
        }
        return builder.toString();
    }

    public String decimalString(Long size) {
        size = this.bytes * size;
        StringBuilder builder = new StringBuilder();
        if (size < MB.bytes) {
            double fileSizeFloat = (double) size / KB.bytes;
            builder.append(String.format("%.2f", fileSizeFloat)).append(KB.describe);
        } else if (size < GB.bytes) {
            double fileSizeFloat = (double) size / MB.bytes;
            builder.append(String.format("%.2f", fileSizeFloat)).append(MB.describe);
        } else if (size < TB.bytes) {
            double fileSizeFloat = (double) size / GB.bytes;
            builder.append(String.format("%.2f", fileSizeFloat)).append(GB.describe);
        } else {
            double fileSizeFloat = (double) size / TB.bytes;
            builder.append(String.format("%.2f", fileSizeFloat)).append(TB.describe);
        }
        return builder.toString();
    }

    public Long convertUnit(Long size, DataUnitENUM target) {
        return this.bytes * size / target.bytes;
    }

    public Double decimalConvertUnit(Long size, DataUnitENUM target) {
        return Double.parseDouble(String.format("%.2f", this.bytes * size / (double) target.bytes));
    }

    DataUnitENUM(String describe, Long bytes) {
        this.describe = describe;
        this.bytes = bytes;
    }
}
